﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;

namespace RaisingForce.Raise.Language.RStates
{
    sealed public class RStateContext :
        IEquatable<RStateContext>,
        IEquatable<IRStateContextState>,
        INotifyPropertyChanged,
        IRStateMutableContextState
    {
        #region Constructors

        public RStateContext()
        {
            this.currentState = new Internal.RStateMutableContextState();
            this.registerStateChangedHandler();
        }

        public RStateContext(IRStateContextState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }
            Contract.EndContractBlock();
            this.currentState = new Internal.RStateMutableContextState(state);
            this.registerStateChangedHandler();
        }

        #endregion

        #region Private Declarations

        private readonly Internal.RStateMutableContextState currentState;

        private IRStateContextState currentStateReadOnly;

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.currentState != null);
        }

        private void registerStateChangedHandler()
        {
            this.currentState.PropertyChanged += this.onCurrentStateChanged;
        }

        private void onCurrentStateChanged(
            object sender,
            PropertyChangedEventArgs e)
        {
            this.currentStateReadOnly = null;
        }

        private IRStateContextState getCurrentStateReadOnly()
        {
            if (this.currentStateReadOnly == null)
            {
                this.currentStateReadOnly =
                    new Internal.RStateMutableContextState(this.currentState)
                    .AsReadOnly();
            }
            return this.currentStateReadOnly;
        }

        #endregion

        #region Base Overrides

        public override bool Equals(object obj)
        {
            return obj is RStateContext ?
                this.Equals((RStateContext)obj) :
                obj is IRStateContextState ?
                this.Equals((IRStateContextState)obj) :
                false;
        }

        public override int GetHashCode()
        {
            return this.currentState.GetHashCode();
        }

        #endregion

        #region Public Methods

        [Pure]
        public RStateContext Clone()
        {
            Contract.Ensures(Contract.Result<RStateContext>() != null);
            return new RStateContext(this.currentState);
        }

        #endregion

        #region IEquatable<RStateContext> Members

        [Pure]
        public bool Equals(RStateContext other)
        {
            return other != null ?
                this.currentState.Equals(other.currentState) :
                false;
        }

        #endregion

        #region IEquatable<IRStateContextState> Members

        [Pure]
        public bool Equals(IRStateContextState other)
        {
            return other != null ?
                this.currentState.Equals(other) :
                false;
        }

        #endregion

        #region IRStateMutableContextState Members

        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                this.currentState.PropertyChanged += value;
            }
            remove
            {
                this.currentState.PropertyChanged -= value;
            }
        }

        public CounterState CounterState
        {
            get
            {
                return this.currentState.CounterState;
            }
            set
            {
                this.currentState.CounterState = value;
            }
        }

        public CounterValue CounterValue
        {
            get
            {
                return this.currentState.CounterValue;
            }
            set
            {
                this.currentState.CounterValue = value;
            }
        }

        public CounterInitializedState CounterInitializedState
        {
            get
            {
                return this.currentState.CounterInitializedState;
            }
            set
            {
                this.currentState.CounterInitializedState = value;
            }
        }

        public CurrentChain CurrentChain
        {
            get
            {
                return this.currentState.CurrentChain;
            }
            set
            {
                this.currentState.CurrentChain = value;
            }
        }

        public PreviousChain PreviousChain
        {
            get
            {
                return this.currentState.PreviousChain;
            }
            set
            {
                this.currentState.PreviousChain = value;
            }
        }

        public ObjectPosition ObjectPosition
        {
            get
            {
                return this.currentState.ObjectPosition;
            }
            set
            {
                this.currentState.ObjectPosition = value;
            }
        }

        public IRStateContextState AsReadOnly()
        {
            return this.getCurrentStateReadOnly();
        }

        #endregion
    }
}
